import ChiVoxSDK from 'chivox';
import Config from './config';

import $ from 'jquery';
import '../css/chivox-example.css';

let mode = ChiVoxSDK.MODE.HTML5;
//let mode = ChiVoxSDK.MODE.FLASH;

let __PREDs = [{
        qid: "PAPER-120501-QT-00001",
        lm: 'Last summer, I went to London, the capital of Britain. It is a beautiful city. Most people speak English and take the underground to work. The most famous river is Thames. It was also my favourite place during my trip. There is a lot of rain in London. There is time difference between London and Beijing. Festivals in London are the same as those in other European countries. I learned a lot about London during the trip. '
    },
    {
        qid: "PAPER-000000-QT-000002",
        lm: "I have a friend who has a large police dog named Tom. Every Sunday afternoon my friend takes Tom for a long walk in the park. One Sunday afternoon a young man came to visit my friend. He stayed a long time. He talked and talked. Soon it was time for my friend to take Tom for his walk. But the visitor still stayed. Tom became very worried about his walk in the park. He walked around the room several times and then sat down directly in front of the visitor and looked at him. But the visitor paid no attention. He continued talking. Finally Tom could stand it no longer. He went out of the room and came back a few minutes later. He sat down again in the front of the visitor but this time he held the visitor's hat in his mouth!"
    },
    {
        qid: "PAPER-000000-QT-000003",
        lm: 'One of my grandsons, when he was about seven, liked to tell a story about two cows in a field. One cow said "Moo", to which the other replied: "Oh, I was just about to say that."  That is an example of the kind of innocent joke that one hardly ever hears nowadays. It is not "cutting edge". It does not "push the boundaries". It is just pleasantly funny in a completely  unchallenged kind of way.'
    }
];

let _idx = 0;
$(".currentWord").html(__PREDs[_idx].lm);

let rank = parseFloat($('#rank').val());
let precision = parseFloat($('#precision').val());

$('.change').click(() => {
    _idx = (_idx + 1) % __PREDs.length;
    $(".currentWord").html(__PREDs[_idx].lm);
});

let showParagraphText = (data, i) => {
    var html = '';
    var a = data[i].content.split("\n");
    for (var p = 0; p < a.length; p++) {
        html += '<p>' + a[p] + '</p>';
    }
    $("#content .currentParagraph").html(html);
};

let getParagraphText = (obj) => {
    var text = obj.content.replace(/\n/g, " ");
    return text;
};
let getDuration = (refText) => {
    return 2000 + 400 * refText.match(/[\d\w']+/gi).length;
};

let getScoreRank = (score) => {
    if (score == 4) {
        return "A";
    } else if (score == 3) {
        return "B";
    } else if (score == 2) {
        return "C";
    } else if (score == 1) {
        return "D";
    } else {
        return "D";
    }
};

let sdk = new ChiVoxSDK({
    appKey: Config.appKey,
    mode: mode,
    sigurl: "/token",
    data: {
        audioUrl: "",
        duration: 60000,
        serverParams: {
            coreType: "en.pred.exam",
            refText: __PREDs[_idx],
            rank: rank,
            precision: precision,
            userId: "chivox tester",
            client_params: {
                ext_subitem_rank4: 0,
                ext_word_details: 1,
                ext_cur_snt: 1
            }
        }
    },

    onInit: (errno, err) => {
        console.info('[ onInit ]:', errno, err);
    },

    onError: (err) => {
        console.error('[ onError ]:', err);
    },

    onBeforeRecord: () => {
        rank = parseFloat($('#rank').val());
        precision = parseFloat($('#precision').val());

        sdk.options.data.serverParams.refText = __PREDs[_idx];
        sdk.options.data.serverParams.rank = rank;
        sdk.options.data.serverParams.precision = precision;

        $("#scoreResult .overall").empty();
        $("#scoreResult .pron").empty();
        $("#scoreResult .integrity").empty();
        $("#scoreResult .fluency").empty();
        $("#scoreResult .details").empty();
        $("#scoreResult .tips").empty();
    },

    onScore: (data) => {
        $("#scoreResult .overall").html(data.result.overall);
        $("#scoreResult .pron").html(getScoreRank(data.result.pron));
        $("#scoreResult .integrity").html(getScoreRank(data.result.integrity));
        $("#scoreResult .fluency").html(getScoreRank(data.result.fluency));
        $("#scoreResult .tips").html(data.result.tips);

        var html = '';
        html += '<table class="table table-hover table-condensed">';
        for (var i = 0; i < data.result.details.length; i++) {
            html += '<tr>';
            html += '<td style="text-align:left;">' + data.result.details[i].text + '</td>';
            html += '<td>' + getScoreRank(data.result.details[i].score) + '</td>';
            html += '</tr>';
        }
        html += '</table>';
        $("#scoreResult .details").html(html);
    },

    onScoreError: (errorType) => { //评分失败的显示 "TIMEOUT", "NO_DATA", ErrorID
        var errorObj = ChiVoxSDK.StatusCode.get(errorType, "cn");
        alert(errorObj.feedback);
    }
});
